package com.xl.controller;


import com.alibaba.fastjson2.JSON;
import com.xl.annotation.Log;
import com.xl.entity.User;
import com.xl.myEnum.OperationEnum;
import com.xl.utils.ServletUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private static Map map = new HashMap();

    static {
        map.put("code",200);
        map.put("msg","success");
    }

    @PostMapping("/login")
    @Log(model = "用户模块", operationType = OperationEnum.LOGIN )
    public Map login(@RequestBody User user){
        System.out.println("用户登录成功:【" + user + "】");
        return map;
    }

    @PutMapping("/save")
    @Log(model = "用户模块", operationType = OperationEnum.SAVE)
    public String save(@RequestBody User user) {
        System.out.println("用户保存完毕:【" + user + "】");
        return JSON.toJSONString(map);
    }

    @DeleteMapping("/delete/{ids}")
    @Log(model = "用户模块", operationType = OperationEnum.DELETE)
    public String delete(@PathVariable("ids") Long[] ids, HttpServletRequest request){
        System.out.println("用户删除中..."+ Arrays.asList(ids));
        Map<String, String> paramMap = ServletUtils.getParamMap(request);
        return JSON.toJSONString(map);
    }

    @PostMapping("/update")
    @Log(model = "用户模块", operationType = OperationEnum.UPDATE)
    public String update(@RequestBody User user) {
        System.out.println("用户更新完毕:【" + user + "】");
        return JSON.toJSONString(map);
    }

    @GetMapping("/list")
    @Log(model = "用户模块", operationType = OperationEnum.QUERY ,excludeParamNames = {"salary"}) //请求对象中的salary字段不会被记录到日志中
    public List<User> list(User user) {
        System.out.println("查询条件：【" + user + "】");
        return User.getUsers();
    }

    @PostMapping("/export")
    @Log(model = "用户模块", operationType = OperationEnum.EXPORT)
    public void export() {
        System.out.println("数据导出完毕！");
    }

    @PostMapping("/import")
    @Log(model = "用户模块", operationType = OperationEnum.IMPORT)
    public void importFile(@RequestBody MultipartFile file){
        System.out.println("文件导入成功");
    }

    @GetMapping("/exception")
    @Log(model = "用户模块")
    public void createException(){
        System.out.println("可能要出现异常了哦");
        int a = 1/0;
    }


}
